<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>03_JS的数据类型</title>
</head>
<body>
<script>
  //JS的数据类型
  //数据类型=原始类型+引用类型
  //原始类型:number,string,boolean,null,undefined
  var a = 1;
  var b = 3.14;
  console.log(typeof a,typeof b);
  var c = 'hello';
  var d = 'world';
  console.log(typeof c,typeof d);
  var e = true;
  var f = false;
  console.log(typeof e,typeof f);
  var g;
  console.log(typeof g);

  //引用类型:object,array,function,Data
  let obj = {};
  console.log(obj);//{}
  console.log(typeof obj);//object
  obj.name = '张三';
  obj.age = 18;
  console.log(obj);
  console.log(obj.name,obj.age);
  let obj2 = {
    name: '张三',
    age: 18,
    sex: '男',
  }
  console.log(obj2);
  console.log(obj2.name,obj2.age,obj2.sex);
  console.log(obj2.play);//访问对象不存在的属性，会得到undefined

  //数组
  let arr = [1,2,3,4,5];
  console.log(arr);
  console.log(arr[0],arr[1],arr[2],arr[3],arr[4]);
  console.log(arr.length);//数组的长度
  console.log(arr[100]);//访问数组不存在的下表，也会得到undefined

  //Number():将其他数据类型转换为数字类型,如果转换失败,会得到NaN（Not a Number）
  console.log(Number(true));//1
  console.log(Number(false));//0
  console.log(Number('123'));//123
  console.log(Number(23.45));//23.45
  console.log(Number('123a'));//NaN

  //Boolean():将其他数据类型转换为布尔类型,如果转换失败,会得到false
  //0,NaN,null,undefined,'',false 这六个值会转换为false
  console.log(Boolean(123));//true
  console.log(Boolean('hello'));//true
  console.log(Boolean(''));//false
  console.log(Boolean(' '));//true
  console.log(Boolean(0));//false
  console.log(Boolean(null));//false
  console.log(Boolean(NaN));//false
  console.log(Boolean(undefined));//false
</script>
</body>
</html>
